﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlTypes;
using System.Data.SqlClient;
using System.Data;

namespace FreeProperty.Data
{
    public class UserInfo : NameValue
    {
        public SqlGuid ID { get; set; }
        public string EmailID { get; set; }
        public string Password { get; set; }
        public string Name { get; set; }
        public string ContactNo { get; set; }
        public SqlBoolean Inactive { get; set; }
        public string LoginError { get; set; }
        
        public SqlInt32 Save()
        {
            SqlInt32 iError = 0;
            try
            {
                SqlCommand cmd = GetSPCommand("createUser");
                cmd.Parameters.Add(new SqlParameter("@prmID", ID));
                cmd.Parameters.Add(new SqlParameter("@prmEmailID", EmailID));
                cmd.Parameters.Add(new SqlParameter("@prmPassword", Cryptography.Encrypt(Password)));
                cmd.Parameters.Add(new SqlParameter("@prmName", Name));
                cmd.Parameters.Add(new SqlParameter("@prmContactNo", ContactNo));
                SqlParameter param = cmd.Parameters.Add(new SqlParameter("@prmError",SqlDbType.Int));
                param.Direction = ParameterDirection.Output;
                cmd.ExecuteNonQuery();

                iError = ToSqlInt32(cmd.Parameters["@prmError"].Value);

            }
            finally
            {
                CloseConnection();
            }

            return iError;
        }
        public UserInfo ValidateLogin()
        {
            SqlInt32 iError = 0;
            UserInfo oUserInfo = new UserInfo();
            try
            {
                SqlCommand cmd = GetSPCommand("validateLogin");
                cmd.Parameters.Add(new SqlParameter("@prmEmailID", EmailID));
                cmd.Parameters.Add(new SqlParameter("@prmPassword", Cryptography.Encrypt(Password)));
                SqlParameter param = cmd.Parameters.Add(new SqlParameter("@prmError", SqlDbType.Int));
                param.Direction = ParameterDirection.Output;
                SqlDataReader reader = cmd.ExecuteReader();
                if (reader.Read())
                {
                    ID = ToSqlGuid(reader["ID"]);
                    Name = ToString(reader["Name"]);
                    ContactNo = ToString(reader["ContactNo"]);
                }
                reader.Close();
                reader = null;
                iError = ToSqlInt32(cmd.Parameters["@prmError"].Value);
                if (iError > 0)
                {
                    oUserInfo.LoginError = GetErrorMessage(iError);
                }
            }
            finally
            {
                CloseConnection();
            }

            return oUserInfo;
        }

        public string GetErrorMessage(SqlInt32 iError)
        {
            string errorMessage = "";
            switch (iError.Value)
            {
                case 1:
                    errorMessage = "Your account is Inactive.";
                    break;
                case 2:
                    errorMessage = "Wrong password.";
                    break;
                case 3:
                    errorMessage = "This email id is not found in the system.";
                    break;
                case 11:
                    errorMessage = "This email id already exists in the system.";
                    break;
                default:
                    errorMessage = "Login error: Please contat admin to verify your account.";
                    break;
            }
            return errorMessage;
        }


    }
}
